package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.globle.Constant;
import com.zlzxm.shoesmarket.util.ZStringHelp;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.StartBundleKey;
import com.zlzxm.shoesmarket.view.activity.manage.bundle.BundleGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.ADGoodsSizeOperate;
import com.zlzxm.shoesmarket.view.adapter.ADGoodsSizeTag;
import com.zlzxm.shoesmarket.view.widget.GoodsInfoView;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMBatchInOut;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityTransfer;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/20/21
 * <br>
 *    批量出入库
 */
public class ACBatchInOut extends  BaseActivity implements OnItemClickListener, OnItemChildClickListener,View.OnClickListener {


    public static void start(Context context, BundleGoodsInfo bundleGoodsInfo) {
        Intent starter = new Intent(context, ACBatchInOut.class);
        starter.putExtra(StartBundleKey.KEY_GOODS_INFO,bundleGoodsInfo);
        context.startActivity(starter);
    }

    VMBatchInOut vmBatchInOut;

    GoodsInfoView goodsInfoView;

    private RecyclerView rvSize,rvSizeDetail;

    private ADGoodsSizeTag adGoodsSizeTag = new ADGoodsSizeTag();

    private ADGoodsSizeOperate adGoodsSizeOperate = new ADGoodsSizeOperate();


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_batch_in_out);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("批量出入库");
        simpleHead.setOnItemclicklistener(this::finish);
        goodsInfoView  = ZViewHelp.findById(this,R.id.goodsInfoView);

        rvSize = ZViewHelp.findById(this,R.id.rvSize);
        rvSizeDetail = ZViewHelp.findById(this,R.id.rvSizeDetail);
        ZViewHelp.setOnClickListener(this,R.id.tvSure,this);

        rvSize.setLayoutManager(new FlexboxLayoutManager(this));

        rvSizeDetail.setLayoutManager(new LinearLayoutManager(this));

        adGoodsSizeTag.setOnItemClickListener(this);

        adGoodsSizeOperate.setOnItemChildClickListener(this);
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmBatchInOut = new ViewModelProvider(this).get(VMBatchInOut.class);

        vmBatchInOut.liveGoodsInfo.observe(this,this::goodsInfoChanged);

        vmBatchInOut.liveOperateSize.observe(this, it -> adGoodsSizeOperate.setList(it));
    }

    @Override
    protected void initData() {
        super.initData();
        updateInfo();
        rvSizeDetail.setAdapter(adGoodsSizeOperate);
        rvSize.setAdapter(adGoodsSizeTag);
    }



    private void updateInfo(){

        BundleGoodsInfo bundleGoodsInfo = (BundleGoodsInfo) getIntent().getSerializableExtra(StartBundleKey.KEY_GOODS_INFO);

        EntityTransfer entityTransfer = new EntityTransfer();
        entityTransfer.setBrand(bundleGoodsInfo.getBrand());
        entityTransfer.setCode(bundleGoodsInfo.getCode());
        entityTransfer.setFace(bundleGoodsInfo.getFace());
        entityTransfer.setName(bundleGoodsInfo.getName());
        entityTransfer.setTag(bundleGoodsInfo.getTags());

        vmBatchInOut.liveGoodsInfo.setValue(entityTransfer);
    }


    private void goodsInfoChanged(EntityTransfer entityTransfer){

        goodsInfoView.updateBrand(entityTransfer.getBrand());
        goodsInfoView.updateCode(entityTransfer.getCode());
        goodsInfoView.updateFace(entityTransfer.getFace());
        goodsInfoView.updateName(entityTransfer.getName());

        List<String> strings = ZStringHelp.splitToList(entityTransfer.getTag(), Constant.Split.size_split);

        List< ADGoodsSizeTag.EntityGoodsSizeTag> tags = new ArrayList<>();

        for (String string : strings) {

            ADGoodsSizeTag.EntityGoodsSizeTag entityGoodsSizeTag = new ADGoodsSizeTag.EntityGoodsSizeTag(string,string);


            tags.add(entityGoodsSizeTag);
        }

        adGoodsSizeTag.setList(tags);

    }


    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

        boolean show = adGoodsSizeTag.setSelectPosition(position);

        vmBatchInOut.editSize(adGoodsSizeTag.getData().get(position).getId(),show);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {

        if(view.getId() == R.id.ivSub){

            if(adGoodsSizeOperate.getData().get(position).sub()){

                adGoodsSizeOperate.notifyDataSetChanged();
            }

        }else if(view.getId() == R.id.ivAdd){

            adGoodsSizeOperate.getData().get(position).add();
            adGoodsSizeOperate.notifyDataSetChanged();
        }


    }

    @Override
    public void onClick(View v) {

        if(v.getId() == R.id.tvSure){

            vmBatchInOut.requestAdd();

        }

    }
}
